<template>
  <div class="news-detail">
    <div class="title wrapper_1200" v-if="newsDetail">
      <div class="home">
      </div>
      <div class="content">
        <div class="title">{{ newsDetail.title }}</div>
        <div class="info">
          <div class="flex">
            <img class="icon" src="../assets/images/time.png" alt="" />
            {{ newsDetail.add_time }}
          </div>
          <div class="flex">
            <img class="icon" src="../assets/images/watch.png" alt="" />
            {{ newsDetail.visit }}
          </div>
        </div>
        <div class="content-text" v-html="newsDetail.content"></div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "news_detail",
  auth: false,
  data() {
    return {
      newsDetail: null
    };
  },
  // 监听路由变化

  watch: {
    $route: {
      handler: function (to, from) {
        this.getNewsDetail();
      },
      immediate: true
    }
  },
  async asyncData({ app, query }) {
    let [categoryMsg] = await Promise.all([
      //获取banner分類
      app.$axios.get(`pc/get_news_detail/${query.id}`)
    ]);
    console.log(categoryMsg);
    return {
      newsDetail: categoryMsg.data
    };
  },
  fetch({ store }) {
    store.commit("isHeader", true);
    store.commit("isFooter", true);
  },
  head() {
    return {
      title: "新聞咨询-" + this.newsDetail.title
    };
  },
  mounted() {
    // 確保頁面跳轉後滾動到頂部
    window.scrollTo(0, 0);
  },
  methods: {
    goDetail(item) {
      let path = item.presale
        ? (path = {
          path: "/goods_presell_detail",
          query: {
            id: item.id
          }
        })
        : (path = { path: `/goods_detail/${item.id}` });
      this.$router.push(path);
    },
    getNewsDetail() {
      this.$axios
        .get(`pc/get_news_detail/${this.$route.query.id}`)
        .then(res => {
          this.newsDetail = res.data;
        })
        .catch(function (err) {
          this.$message.error(err);
        });
    }
  }
};
</script>

<style scoped lang="scss">
.news-detail {
  background: var(--bg-page);
  min-height: 100vh;
  padding-top: 55px;
  padding-bottom: 40px;
}

.wrapper_1200 {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.home {
  padding: 20px 0;
  color: var(--text-color);
  font-size: 14px;
  font-weight: 400;
  border-radius: 8px;
  margin-bottom: 20px;
  padding-left: 20px;
  display: none;

  a {
    color: var(--text-color);
    text-decoration: none;
    transition: color 0.3s ease;

    &:hover {
      color: $primary-base;
    }
  }

  .news-title {
    color: $text-medium-gray;
    font-weight: 500;
  }
}

.content {
  overflow: hidden;
  margin-top: 60px;

  .title {
    padding: 40px 50px 30px;
    text-align: center;
    font-weight: 600;
    font-size: 28px;
    color: var(--text-color);
    line-height: 1.4;
    margin-bottom: 0;
    position: relative;

  }
}

.info {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px 0;
  background: var(--bg-light);
  border-bottom: 1px solid var(--border-color);
  gap: 40px;

  .flex {
    display: flex;
    align-items: center;
    color: var(--text-color);
    font-size: 14px;
    padding: 8px 16px;
    border-radius: 20px;
    transition: all 0.3s ease;

    &:hover {
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .icon {
      width: 18px;
      height: 18px;
      margin-right: 8px;
      opacity: 0.7;
    }
  }
}

.content-text {
  padding: 50px;
  line-height: 1.8;

  // 美化動態內容的樣式
  :deep(h1),
  :deep(h2),
  :deep(h3),
  :deep(h4),
  :deep(h5),
  :deep(h6) {
    color: var(--text-color);
    margin: 32px 0 20px 0;
    font-weight: 600;
    line-height: 1.4;
  }

  :deep(h1) {
    font-size: 28px;
    border-bottom: 2px solid #eee;
    padding-bottom: 12px;
    color: $primary-base;
  }

  :deep(h2) {
    font-size: 24px;
    color: var(--text-color);
    position: relative;
    padding-left: 20px;

    &::before {
      content: '';
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 4px;
      height: 20px;
      background: $primary-base;
      border-radius: 2px;
    }
  }

  :deep(h3) {
    font-size: 20px;
    color: var(--text-color);
  }

  :deep(p) {
    color: var(--text-color);
    line-height: 1.8;
    margin: 16px 0;
    font-size: 15px;
    text-align: justify;
  }

  :deep(ul),
  :deep(ol) {
    margin: 20px 0;
    padding-left: 24px;

    li {
      color: var(--text-color);
      line-height: 1.8;
      margin: 12px 0;
      font-size: 15px;
      position: relative;
    }
  }

  :deep(ul li) {
    &::before {
      content: '•';
      color: $primary-base;
      font-weight: bold;
      position: absolute;
      left: -16px;
    }
  }

  :deep(strong),
  :deep(b) {
    color: var(--text-color);
    font-weight: 600;
  }

  :deep(a) {
    color: $primary-base;
    text-decoration: none;
    transition: all 0.3s ease;
    border-bottom: 1px solid transparent;

    &:hover {
      color: $primary-hover;
      border-bottom-color: $primary-base;
    }
  }

  :deep(blockquote) {
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    border-left: 4px solid $primary-base;
    padding: 20px 24px;
    margin: 24px 0;
    color: var(--text-color);
    font-style: italic;
    border-radius: 0 8px 8px 0;
    position: relative;

    &::before {
      content: '"';
      position: absolute;
      top: 10px;
      left: 10px;
      font-size: 24px;
      color: $primary-base;
      opacity: 0.5;
    }
  }

  :deep(table) {
    width: 100%;
    border-collapse: collapse;
    margin: 24px 0;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);

    th,
    td {
      border: 1px solid var(--border-color);
      padding: 16px 20px;
      text-align: left;
      font-size: 14px;
    }

    th {
      background: var(--bg-light);
      color: var(--text-color);
      font-weight: 600;
    }

    tr:nth-child(even) {
      background: var(--bg-light);
    }

    tr:hover {
      background: var(--bg-hover);
    }
  }

  :deep(img) {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    margin: 20px 0;
  }

  :deep(code) {
    background: var(--bg-light);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', monospace;
    color: var(--primary-base);
    font-size: 13px;
  }

  :deep(pre) {
    background: var(--bg-light);
    padding: 20px;
    border-radius: 8px;
    overflow-x: auto;
    border-left: 4px solid var(--primary-base);

    code {
      background: none;
      padding: 0;
      color: var(--text-color);
    }
  }
}


// 響應式設計
@media (max-width: 768px) {
  .wrapper_1200 {
    padding: 0 15px;
  }

  .content {
    margin-top: 80px;
    border-radius: 12px;

    .title {
      padding: 30px 20px 20px;
      font-size: 24px;
    }

    .info {
      padding: 15px 20px;
      flex-direction: column;
      gap: 15px;
    }

    .content-text {
      padding: 30px 20px;

      :deep(h1) {
        font-size: 24px;
      }

      :deep(h2) {
        font-size: 20px;
      }

      :deep(h3) {
        font-size: 18px;
      }
    }
  }
}
</style>
